﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Admin.Models;
using System.Data;
namespace BookStore.Admin.Controllers
{
    [HandleError]
    public class AccountController : Controller
    {
        
        // view edit user
        public ActionResult EditUser()
        {
            UsersModel tbAccount = new UsersModel();

            if (Session["Session_Admin"] == null)
                Response.Redirect("/admin/login");
            if (this.Request.Form.Count != 0)
            {

                string giatri = null;
                string[] temp = new string[8];
                temp[0] = Server.HtmlEncode(this.Request.Form["address"]); ;
                temp[1] = Server.HtmlEncode(this.Request.Form["phone"]);
                temp[2] = Server.HtmlEncode(this.Request.Form["cmnd"]);
                temp[3] = Server.HtmlEncode(this.Request.Form["email"]);
                temp[4] = Utils.Now().ToString(); // update date
                temp[5] = null;
                temp[6] = null;
                temp[7] = null;
                giatri = tbAccount.Edit_Account(tbAccount.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(Session["Session_Admin"].ToString()), temp).ToString();
                if (giatri == "1")
                {
                    ViewData["success"] = "Hệ thống đã cập nhật thành công";
                }
                else if (giatri == "-2")
                {
                    ViewData["error"] = "Email đã có người sử dụng";
                }
                else if (giatri == "-3")
                {
                    ViewData["error"] = "Hệ thống đang bận. vui lòng trở lại sau";
                } 
            }
            if (Session["Session_Admin"] != null)
            {
                int id = Convert.ToInt32(Session["Session_Admin"].ToString());
                string[] result = new string[6];
                result = tbAccount.getDetailUserById(id);
                if (result == null) Response.Redirect("/admin/login");
                ViewData["email"] = result[0];
                ViewData["phone"] = result[1];
                ViewData["cmnd"] = result[2];
                ViewData["address"] = result[3];
                ViewData["user_name"] = result[4];
            }
            else Response.Redirect("/admin/login");

            return View();
        }

        
        // view change password
        public ActionResult ChangePassword()
        {
            UsersModel user = new UsersModel();
            if (Session["Session_Admin"] == null)
            {
                // chuyen den trang logon
                Response.Redirect("/admin/login");
            }
            if (this.Request.Form.Count != 0)
            {
                UsersModel tbAccount = new UsersModel();
                string old_password = Server.HtmlEncode(this.Request.Form["old_password"]);
                string new_password = Server.HtmlEncode(this.Request.Form["new_password"]);
                string giatri;
                giatri = tbAccount.Change_Password(Convert.ToInt32(Session["Session_Admin"].ToString()), Utils.GetMD5Hash(old_password), Utils.GetMD5Hash(new_password)).ToString();
                if (giatri == "1")
                {
                    ViewData["success"] = "Hệ thống đã thay đổi mật khẩu thành công";
                }
                else
                {
                    ViewData["error"] = "Bạn đã nhập sai mật khẩu";
                }
            }
            ViewData["user_name"] = user.getUserNamebyID(Convert.ToInt32(Session["Session_Admin"]));
            return View();

        }
        public ActionResult ListUser()
        {
            UsersModel user = new UsersModel();
            if (Session["Session_Admin"] == null)
            {
                // chuyen den trang logon
                Response.Redirect("/admin/login");

            }
            if (Session["listser_message"] != null)
            {
                ViewData["success"] = Session["listser_message"];
                Session["listser_message"] = null;
            }
            ViewData["user_name"] = user.getUserNamebyID(Convert.ToInt32(Session["Session_Admin"]));
            string[][] temp;
            temp = user.getListUser(user.CheckAccount(Convert.ToInt32(Session["Session_Admin"])),Convert.ToInt32(Session["Session_Admin"]));
            if (temp == null)
                ViewData["result"] = null;
            else
                ViewData["result"] = temp;
            return View();

        }
        public ActionResult InsertUser()
        {
            UsersModel user = new UsersModel();
            if (Session["Session_Admin"] == null)
            {
                // chuyen den trang logon
                Response.Redirect("/admin/login");

            }

            if (this.Request.Form.Count != 0)
            {
                UsersModel tbAccount = new UsersModel();
                Random ran = new Random();
                string giatri = "";
                int n = 10000;
                string value = ran.Next(n).ToString();

                string[] temp = new string[12];
                temp[0] = Server.HtmlEncode(this.Request.Form["username"]);
                temp[1] = Utils.GetMD5Hash(Server.HtmlEncode(this.Request.Form["password"]));
                temp[2] = Server.HtmlEncode(this.Request.Form["address"]);
                temp[5] = Server.HtmlEncode(this.Request.Form["phone"]);
                temp[4] = Server.HtmlEncode(this.Request.Form["cmnd"]);
                temp[3] = Server.HtmlEncode(this.Request.Form["email"]);
                temp[6] = Server.HtmlEncode(this.Request.Form["username"]);
                temp[7] = Utils.Now().ToString(); // create date
                temp[8] = Utils.Now().ToString(); // update date
                temp[9] = Server.HtmlEncode(this.Request.Form["status"]);
                temp[10] = Utils.GetMD5Hash(value); // active code
                temp[11] = Server.HtmlEncode(this.Request.Form["user_type"]);

                giatri = tbAccount.Add_Account(temp).ToString();
                if (giatri == "1")
                {
                    ViewData["success"] = "Hệ thống đã thêm thành công";
                }
                else if (giatri == "-1")
                {
                    ViewData["error"] = "Tên đăng nhập đã có người sử dựng";
                }
                else if (giatri == "-2")
                {
                    ViewData["error"] = "Email đã có người sử dụng";
                }
                else if (giatri == "-3")
                {
                    ViewData["error"] = "Hệ thống đang bận. vui lòng trở lại sau";
                } 
                
            }
            ViewData["type"] = user.CheckAccount(Convert.ToInt32(Session["Session_Admin"]));
            return View();

        }

        
        public ActionResult UpdateUser()
        {
            UsersModel tbAccount = new UsersModel();
            string giatri = "";
            string user_id = "";
            if (Session["Session_Admin"] == null) Response.Redirect("/admin/account/listuser");
            
            if (this.Request.Form.Count != 0)
            {
               
                string[] temp = new string[8];
                temp[0] = Server.HtmlEncode(this.Request.Form["address"]);
                temp[1] = Server.HtmlEncode(this.Request.Form["phone"]);
                temp[2] = Server.HtmlEncode(this.Request.Form["cmnd"]);
                temp[3] = Server.HtmlEncode(this.Request.Form["email"]);
                temp[4] = Utils.Now().ToString();
                temp[5] = Server.HtmlEncode(this.Request.Form["password"]) != "" ? Utils.GetMD5Hash(Server.HtmlEncode(this.Request.Form["password"])).ToString() : null;
                temp[6] = Server.HtmlEncode(this.Request.Form["status"]); 
                temp[7] = Server.HtmlEncode(this.Request.Form["user_type"]) != "" ? Server.HtmlEncode(this.Request.Form["user_type"]) : null; ;
                giatri = tbAccount.Edit_Account(tbAccount.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(Server.HtmlEncode(this.Request.Form["id"])), temp).ToString();
                if (giatri == "1")
                {
                    ViewData["success"] = "Hệ thống đã thêm thành công";
                }
                else if (giatri == "-2")
                {
                    ViewData["error"] = "Email đã có người sử dụng";
                }
                else if (giatri == "-3")
                {
                    ViewData["error"] = "Hệ thống đang bận. vui lòng trở lại sau";
                }

            }
            if (Session["Session_Admin"] != null)
            {
                if (this.Request.Form.Count != 0)
                {
                    user_id = Server.HtmlEncode(this.Request.Form["id"]);
                }
                else if (this.Request.QueryString["id"] != null)
                {
                    user_id = this.Request.QueryString["id"];
                }
                if (this.Request.QueryString["id"] != null || this.Request.Form.Count != 0)
                {

                    int id = Convert.ToInt32(user_id.ToString());
                    string[] result = new string[7];
                    result = tbAccount.getDetailUserById(id);
                    if (result == null) Response.Redirect("/admin/login");
                    ViewData["id"] = this.Request.QueryString["id"];
                    ViewData["email"] = result[0];
                    ViewData["phone"] = result[1];
                    ViewData["cmnd"] = result[2];
                    ViewData["address"] = result[3];
                    ViewData["user_name"] = result[4];
                    ViewData["active"] = result[5];
                    ViewData["user_type"] = result[6];
                    ViewData["type"] = tbAccount.CheckAccount(Convert.ToInt32(Session["Session_Admin"]));
                }
                else Response.Redirect("/admin/account/listuser");
            }
            else Response.Redirect("/admin/login");
            return View();

        }
        // check validate and update edit user
        public ActionResult JsonCheckUpdateUser(string phone, string email, string cmnd, string address,string password,string status,string user_type)
        {
            UsersModel tbAccount = new UsersModel();

            if (Session["Session_Admin"] != null)
            {
                if (this.Request.QueryString["id"] != null)
                {
                    string giatri = null;
                    string[] temp = new string[8];
                    temp[0] = address;
                    temp[1] = phone.ToString();
                    temp[2] = cmnd;
                    temp[3] = email;
                    temp[4] = Utils.Now().ToString();
                    temp[5] = password != "" ? Utils.GetMD5Hash(password).ToString() : null;
                    temp[6] = status;
                    temp[7] = user_type != "" ? user_type : null; ;
                    giatri = tbAccount.Edit_Account(tbAccount.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(this.Request.QueryString["id"]), temp).ToString();
                    return Content(giatri);
                }
            }
            return Content("1");
        }
        public ActionResult DeleteUser()
        {
            if (Session["Session_Admin"] == null) return RedirectToAction("index", "login");

            UsersModel user = new UsersModel();

            if (this.Request.QueryString["id"] != null)
            {
                user.DeleteUserByID(user.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(this.Request.QueryString["id"])).ToString();
                this.Session["listser_message"] = "Hệ thống đã xóa thành công";
                Response.Redirect("/admin/account/listuser");
            }
            else if (this.Request.Form["selected[]"] != null)
            {
                foreach (string value in this.Request.Form["selected[]"].Split(','))
                {
                    user.DeleteUserByID(user.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(value));
                }
            }
            this.Session["listser_message"] = "Hệ thống đã xóa thành công";
            Response.Redirect("/admin/account/listuser");
            return RedirectToAction("index", "book");
        }
        public ActionResult JsonCheckDelete()
        {
            UsersModel user = new UsersModel();
            string giatri = "-1";

            if (this.Request.QueryString["id"] != null)
            {
                giatri = user.DeleteUserByID(user.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(this.Request.QueryString["id"])).ToString();
                Response.Redirect("/admin/account/listuser");
                return Content(giatri);
            }
            else
            {
                if (this.Request.QueryString["total"] != null)
                {
                    for (int i = 1; i <= Convert.ToInt32(this.Request.QueryString["total"]); i++)
                    {
                        if (this.Request.QueryString["list_id_" + i] != null)
                        {
                            string id = this.Request.QueryString["list_id_" + i];
                            giatri = user.DeleteUserByID(user.CheckAccount(Convert.ToInt32(Session["Session_Admin"])), Convert.ToInt32(id)).ToString();
                            if (giatri == "-1")
                            {
                                return Content("-1");
                            }
                        }
                        
                    }

                }

                return Content(giatri);
            }

        }
        
       
    }
}
